package com.qianyu.versioncontrolapi.aspect;

import com.qianyu.versioncontrolapi.service.OptionLogService;
import com.qianyu.versioncontrolcore.anno.OperLogAnnotation;
import com.qianyu.versioncontrolcore.entity.base.Result;
import com.qianyu.versioncontrolcore.entity.bean.OptionLog;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Component
@Aspect
@RequiredArgsConstructor
public class OptionLogAspect {

	private final OptionLogService optionLogService;

	@Pointcut(value = "execution(* com.qianyu.versioncontrolapi.controller.*Controller.*(..))")
	private void pointcut01() {
	}

	@AfterReturning(value="@annotation(log)",returning="obj")
	public void afterReturningAdvice(JoinPoint jp, Object obj, OperLogAnnotation log) {
		OptionLog op = new OptionLog();
		op.setMenuName(log.menuName());
		op.setOptionType(log.opeType().getOptionType());
		Result result = (Result) obj;
		op.setVersion(result.getData().toString());
		optionLogService.saveLog(op);
	}
	
}
